﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ncp.Business;
using ncp.Business.Common;

namespace ncp.Web.page
{
    public partial class show : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetNewsContent();
                BindNews();
            }
        }
        private void GetNewsContent()
        {
            var requeryId = Request.QueryString["id"];
            var requeryCode = Request.QueryString["channel"] ?? "";
            int id = 0;
            if (requeryId == null || !int.TryParse(EncryptHelper.DecryptString(requeryId), out id))
            {
                Response.Redirect("/shop/error.html");
            }
            var dicWhere = new Dictionary<string, object> {{"News_Id", id}, {"NC_Code", requeryCode}};
            var preview = Request.QueryString["preview"];
            if (preview==null)
            {
                dicWhere.Add("News_Status", 1);
            }
            var dt = NewsInfoBll.GetNewsInfoTable("*", dicWhere, CommonConst.DB_Name);
            if (dt.Rows.Count == 0)
            {
                Response.Redirect("/shop/error.html");
            }
            else
            {
                ltTitle.Text = dt.Rows[0]["News_Title"].ToString();
                lbTime.Text ="时间："+ Convert.ToDateTime(dt.Rows[0]["News_Date"].ToString()).ToString("yyyy-MM-dd HH:mm:ss");
                if (dt.Rows[0]["News_Source"] != DBNull.Value && dt.Rows[0]["News_Source"].ToString() != "")
                {
                    lbAuthor.Text = "来源：" + dt.Rows[0]["News_Source"];
                }
                else
                {
                    //获取作者
                    var userName = "佚名";
                    if (dt.Rows[0]["Client_Name"] != DBNull.Value && dt.Rows[0]["Client_Name"].ToString() != "")
                    {
                        userName = dt.Rows[0]["Client_Name"].ToString();
                    }
                    if (dt.Rows[0]["User_Name"] != DBNull.Value && dt.Rows[0]["User_Name"].ToString() != "")
                    {
                        userName = dt.Rows[0]["User_Name"].ToString();
                    }
                    lbAuthor.Text = "作者：" + userName;
                }

                ltContent.Text = dt.Rows[0]["News_Note"].ToString();
                //点击
                int click = int.Parse(dt.Rows[0]["News_Times"].ToString());
                lbClick.Text = "点击：" + (click==0?"1":click.ToString());
            }
            //增加文章点次数
            //获取客户端IP地址，判断用户是否点击过该页面
            string loginIP = Request.UserHostAddress.Replace(".","-");
            var cookie = CookieHelper.GetSingleValue(id.ToString(), id.ToString());
            if (cookie!= id.ToString())
            {
                NewsInfoBll.PageClicked(id.ToString(), CommonConst.DB_Name);
                CookieHelper.AddSingleCookie(id.ToString(), id.ToString(), id.ToString());
            }
        }

        private void BindNews()
        {
            var requeryId = Request.QueryString["id"];
            var requeryCode = Request.QueryString["channel"] ?? "";
            int id = 0;
            if (requeryId == null || !int.TryParse(EncryptHelper.DecryptString(requeryId), out id))
            {
                Response.Redirect("/shop/error.html");
            }

            //热门信息
            Dictionary<string, object> dicHot = new Dictionary<string, object>();
            dicHot.Add("NC_Code", requeryCode);
            dicHot.Add("News_Status", 1);
            var dtHot = NewsInfoBll.GetNewsInfoTable("top 10 *", dicHot,"News_Times,", CommonConst.DB_Name,true);
            repHot.DataSource = dtHot;
            repHot.DataBind();
            //推荐信息
            dicHot.Add("more", "CHARINDEX('c',News_Flag,0)>0");
            var dtRec = NewsInfoBll.GetNewsInfoTable("top 10 *", dicHot, "News_Times,", CommonConst.DB_Name,false);
            repRec.DataSource = dtRec;
            repRec.DataBind();
            //随机信息
            dicHot.Remove("more");
            dicHot.Add("more", "News_Id<>"+id);
            var dtRecRandom = NewsInfoBll.GetNewsInfoTable("top 4 *", dicHot, "News_Times,newid(),", CommonConst.DB_Name,true);
            repRandom.DataSource = dtRecRandom;
            repRandom.DataBind();
        }
    }
}
